package com.ruoyi.algorithm.dict.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.algorithm.dict.entity.Dictionary;
import com.ruoyi.algorithm.dict.entity.DictionaryType;
import com.ruoyi.algorithm.dict.repository.DictionaryRepository;
import com.ruoyi.algorithm.dict.repository.DictionaryTypeRepository;
import com.ruoyi.algorithm.dict.service.DictionaryService;
import com.ruoyi.algorithm.util.ReflectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    DictionaryRepository dictionaryRepository;

    @Autowired
    DictionaryTypeRepository dictionaryTypeRepository;

    /**
     * 分页查询字典表
     *
     * @param page       分页参数
     * @param dictionary 查询条件
     * @param orderBy    排序条件
     * @param isAsc      是否正序
     * @return 分页结果
     * @throws NoSuchFieldException 排序字段不存在
     */
    @Override
    public Page<Dictionary> page(Page<Dictionary> page, Dictionary dictionary, String orderBy, Boolean isAsc) throws NoSuchFieldException {
        String[] orderByVar = ReflectionUtil.getTableField(orderBy, Dictionary.class);
        return dictionaryRepository.page(page, Wrappers.query(dictionary).orderBy(Objects.nonNull(orderBy), isAsc, orderByVar));
    }

    /**
     * @param page           分页参数
     * @param dictionaryType 查询条件
     * @param orderBy        排序条件
     * @param isAsc          是否正序
     * @return 分页结果
     * @throws NoSuchFieldException 排序字段不存在
     */
    @Override
    public Page<DictionaryType> pageDictType(Page<DictionaryType> page, DictionaryType dictionaryType, String orderBy, Boolean isAsc) throws NoSuchFieldException {
        String[] tableField = ReflectionUtil.getTableField(orderBy, DictionaryType.class);
        return dictionaryTypeRepository.page(page, Wrappers.query(dictionaryType).orderBy(Objects.nonNull(orderBy), isAsc, tableField));
    }

    /**
     * 根据字典类型key以及字典key查询字典value
     *
     * @param typeKey 字典类型key
     * @param key     字典key
     * @return 字典value
     */
    @Override
    public String getValueByKey(String typeKey, String key) {
        return dictionaryRepository.getValueBykey(typeKey, key);
    }

    /*
     * 字典表增删改
     *
     * */
    @Override
    public void add(Dictionary dictionary) {
        dictionary.setCreateTime(new Date());
        dictionary.setUpdateTime(new Date());
        dictionaryRepository.save(dictionary);
    }

    @Override
    public boolean update(Dictionary dictionary) {
        dictionary.setUpdateTime(new Date());
        return dictionaryRepository.updateById(dictionary);
    }

    @Override
    public void deleteById(Long id) {
        dictionaryRepository.removeById(id);
    }


    /*
     * 字典类型表增删改
     *
     * */
    @Override
    public void addDictType(DictionaryType dictionaryType) {
        dictionaryTypeRepository.save(dictionaryType);
    }

    @Override
    public boolean updateDictType(DictionaryType dictionaryType) {
        return dictionaryTypeRepository.updateById(dictionaryType);
    }

    @Override
    public void deleteDictTypeById(Long id) {
        dictionaryTypeRepository.removeById(id);
    }

    @Override
    public IPage<Dictionary> getDictListByTypeId(IPage<Dictionary> page, String typeKey) {
        //根据typeKey获取TypeId
        Long typeId = dictionaryTypeRepository.getOne(Wrappers.lambdaQuery(DictionaryType.class).eq(DictionaryType::getTypeKey, typeKey)).getId();
        //根据typeId获取dict
        return dictionaryRepository.page(page, Wrappers.lambdaQuery(Dictionary.class).eq(Dictionary::getTypeKeyId, typeId));
    }


}




